﻿using SysHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SysCommon.Extensions;
using MODEL;
using SysHelper.Attributes;

namespace SysController
{
    [AjaxRequest]
    [SkipPemission]
    public class HomeController : Controller
    {
        [HttpGet]
        [Skip]
        [SkipPemission]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [Skip]
        [SkipPemission]
        public ActionResult Login(MODEL.LoginUser user)
        {
            bool isLogin = OperateContext.CurrentContext.Login(user);
            if (isLogin)
            {
                return OperateContext.CurrentContext.RedirectAjax(AjaxMsgStatuEnum.Success, null, null, "/products/product/Index");
            }
            else
            {
                return OperateContext.CurrentContext.RedirectAjax(AjaxMsgStatuEnum.Failure, "登陆失败,请确认账号、密码正确！");
            }
        }
         
        public ActionResult Index(MODEL.PageModel model)
        {
            model = model == null ? new MODEL.PageModel() { PageNumber = 222 } : model;
            return View(model);
        }

        public ActionResult GetPartialCategory(int pid)
        {
            List<MODEL.View_Category> list = OperateContext.CurrentContext.BllSession.IView_Category_BLL.GetListByWhere(s => s.PCategoryID == pid && s.IsDel == false);
            if (pid == 0)
            {
                return PartialView("PartialCategory", list);
            }
            else
            {
                return Json(list, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]
        public ActionResult GetMaxCode(string id)
        {
            string codeLike = id + "%";
            MODEL.Product model = OperateContext.CurrentContext.BllSession.IProduct_BLL
                .GetListByWhere(s => s.Code.Like(codeLike), s => new {s.Code , s.ProductID }, false)
                .FirstOrDefault();
            if (model != null)
                return Json(string.Format(@"当前类最大SKU编号：{0}；SKU 格式：{1} ", model.Code.Substring(2, 3), model.Code), JsonRequestBehavior.AllowGet);
            else
                return Json(string.Format(@"SKU 格式：{0}000A0", id), JsonRequestBehavior.AllowGet);
        }
    }
}
